//
//  TransitionHandle.swift
//  Swift30Days
//
//  Created by licheng ke on 2017/3/9.
//  Copyright © 2017年 licheng ke. All rights reserved.
//

import UIKit

protocol  TransitionHandleDelegate:class{
    func missAction()
}

class TransitionHandle:
    NSObject,
    UIViewControllerAnimatedTransitioning,  //实现代理设置动画对象
    UIViewControllerTransitioningDelegate   //转场的一些设置
{
   override init() {
        super.init()
    }
    
   convenience init(delegate : TransitionHandleDelegate) {
        self.init();
        self.delegate = delegate;
    }
    
    weak var delegate : TransitionHandleDelegate?
    
    //设置动画时间，用于代理，和后面动画时间
    let duration = 0.5;
    
    //用于判断状态
    var isPresenting = true;
    
    //做一个控制，因为在dismiss的时候里面的参数都变了
    var snpaShotView : UIView? {
        didSet{
            if let view = snpaShotView {
                view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(snapTapAction)))
            }
        }
    }
    
    func snapTapAction(){
        if delegate != nil {
            delegate?.missAction();
        }
    }
    
    //MARK: - UIViewControllerTransitioningDelegate
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresenting = true;
        return self;
    }
    
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresenting = false;
        return self;
    }
    
    //MARK: -具体动画对象
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration;
    }
    
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        //首先拿到各个controller的view，然后再拿到容器
        let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)
        let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)
        let container = transitionContext.containerView
        
        //设置变动的位置
        let transformToUp = CGAffineTransform(translationX: 0, y: -70)
        let transformToDown = CGAffineTransform(translationX: 0, y: SCREEN_H - 180)
        
        if isPresenting {
            toView?.transform = transformToUp;
            //加入到container中,注意这里还要加入toView。还有加入的先后问题
            self.snpaShotView = fromView?.snapshotView(afterScreenUpdates: false);
            container.addSubview(toView!);
            container.addSubview(self.snpaShotView!);
        }
        
        //使用普通的UIView动画就行了
        UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.2, options: .curveLinear, animations: {
            if self.isPresenting {
                self.snpaShotView?.transform = transformToDown;
                toView?.transform = CGAffineTransform.identity;
            }
            else {
                self.snpaShotView?.transform = CGAffineTransform.identity;
                fromView?.transform = transformToUp;
            }
            
        }) { (done) in
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            if !self.isPresenting {
                self.snpaShotView?.removeFromSuperview();
            }
        }

    }
    
    
    
    
    
    
    
    
    
    
    
    
}
